import time

from selenium import webdriver
import os
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

"""
1.元素等待
   1)显示等待 
   2）隐式等待
   3）sleep  就是代码遇见sleep(10),就在此等待10S，强制停止运行，等待10S
"""

"""
显示等待:等待某个条件成立时继续执行，否则达到最长等待时间就抛出异常
"""

driver = webdriver.Chrome()
driver.maximize_window()
driver.get("http://beike.zxxk.com/index.html#/")

# ele = WebDriverWait(driver, 10, 0.5).until(EC.presence_of_element_located((By.ID, 'kw')))
#
# ele.send_keys("python")
#
# time.sleep(5)
# driver.quit()


"""
隐式等待:通过一定的时长等待页面元素加载完成，超过时间抛出异常
"""

# driver.implicitly_wait(10)
# driver.find_element_by_id('kw').send_keys('python')
# # ele.send_keys("python")
# driver.switch_to.frame('frame')  # 切换iframe
#
# driver.find_element_by_id('username').send_keys('15814074095')
# driver.find_element_by_name("password").send_keys('15814074095')
# driver.find_element_by_id('CommonLogin').click()
# driver.switch_to.default_content()  # 回到默认页面
# 多表单切换 iframe


# # 多窗口切换
driver.switch_to.frame('frame')  # 切换iframe

driver.find_element_by_id('username').send_keys('15814074095')
driver.find_element_by_name("password").send_keys('wy123456')
driver.find_element_by_id('CommonLogin').click()

# 点击上传按钮

driver.find_element_by_css_selector('#bk-upload').click()

time.sleep(2)
os.system(r"E:\上传.exe")
#
# handle = driver.current_window_handle
# print(handle)
# time.sleep(3)
# driver.find_element_by_css_selector('[href="#/download"]').click()
# handles = driver.window_handles
# print(handles)
# driver.switch_to.window(handles[-1])
#
# print(driver.title)
# driver.find_element_by_css_selector('.iconfankui').click()
# driver.close()

# driver.switch_to.window(handles[0])
# print(driver.title)

# driver.find_element_by_id('s-usersetting-top').click()
# driver.find_element_by_css_selector('.setpref').click()
# time.sleep(2)
#
# driver.find_element_by_xpath('//div[@id="se-setting-7"]//a[text()="保存设置"]').click()
#
# driver.switch_to.alert.accept()

# 执行JavaScript代码

# js = "window.open('https://www.baidu.com/')"
# driver.execute_script(js)
# cookie = driver.get_cookies()
# print(cookie)
#
# cookie 绕过登录
# cookies = [{"domain": "www.ketangpai.com",
#             "name": "PHPSESSID",
#             "value": "B490B5EBF6F3CD402E515D22BCDA1598",
#             "path": '/',
#             "expires": None},
#            {"domain": "www.ketangpai.com",
#             "name": "__guid",
#             "value": "247261698.843954639409993600.1595162581110.1545",
#             "path": '/',
#             "expires": None},
#            {"domain": "www.ketangpai.com",
#             "name": "ketangpai_home_remember",
#             "value": "think%3A%7B%22username%22%3A%22MDAwMDAwMDAwMLOGuZiGubevhd6G27O5jZ4%22%2C%22expire%22%3A"
#                      "%22MDAwMDAwMDAwMLOGuZmHub-vhriKlbGmdZ4%22%2C%22token%22%3A%22MDAwMDAwMDAwMMurrpWavLehhs1"
#                      "-lbOphZaDp3uVepuomcWmmqaMiHtnr5ypzYWosKKZq6HQxtOK0ZCme5p"
#                      "-q6iZu2yrn4uNhJ3KedDYk7ivboS4ipmyuZHYhbeHlX56YW0%22%2C%22sign%22%3A"
#                      "%22c6f7321f148de4b15a4b960bcee53690%22%7D",
#             "path": '/',
#             "expires": None},
#            {"domain": "www.ketangpai.com",
#             "name": "ketangpai_home_slb",
#             "value": "69fe0169927fb731c0cb96fa3ba1db0c",
#             "path": '/',
#             "expires": None},
#            {"domain": "www.ketangpai.com",
#             "name": "monitor_count",
#             "value": "9",
#             "path": '/',
#             "expires": None},
#            {"domain": ".qq.com",
#             "name": "RK",
#             "value": "3fh5ougnan",
#             "path": '/',
#             "expires": None}
#            ]

# for cookie in cookies:
#     driver.add_cookie(cookie)
#
# driver.get('https://www.ketangpai.com/Main/index.html')
# print(driver.get_cookies())

# cookie = driver.get_cookies()
# print(cookie)
# driver.get()
#
# driver.get_screenshot_as_file(r'D:\0login.png')

time.sleep(5)
driver.quit()
